package com.fred.websocket.controller;

import com.fred.websocket.ws.WebScoketServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * websocket controller
 * @Date 2020/4/1 11:15
 * @Author ZhuFeng
 * @Version 2.0
 */
@Controller
public class WSController {

    /**
     * 页面请求
     */
    @GetMapping("/socket/{pid}")
    public ModelAndView socket(@PathVariable String pid) {
        ModelAndView mdv = new ModelAndView("/socket");
        mdv.addObject("pid", pid);
        return mdv;
    }

    /**
     * 推送数据接口
     */
    @ResponseBody
    @RequestMapping("/socket/push/{pid}")
    public String pushToWeb(@PathVariable String pid, String message){
        try {
            WebScoketServer.sendInfo(message,pid);
        }catch (Exception e){
            e.printStackTrace();
            return "推送失败";
        }
        return "发送成功";
    }


}
